﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;


namespace setup
{

    internal class Program
    {
        public static bool FindAndKillProcess(string name)
        {
            var p = Process.GetProcesses();
            foreach (Process clsProcess in p)
            {
                if (clsProcess.ProcessName == name)
                {
                    clsProcess.Kill();
                    return true;
                }
            }
            return false;
        } 
        static  void CheckDirectories(string local)
        {
            FindAndKillProcess("VMDS.GUIClient");
            Console.WriteLine("Checking Dirs....");
            if (!Directory.Exists(local))
            {
                Console.WriteLine("Create "+local);
                Directory.CreateDirectory(local);
            }

            var dirlist = new List<string>() { "data", "OS", "VM", "cache" };
            foreach (var dir in dirlist)
            {
                var path = local +"\\"+ dir;
                if (!Directory.Exists(path))
                {
                    Console.WriteLine("Create "+path);
                    Directory.CreateDirectory(path);
                }
            }
        }

        static void SetPermissions(string path)
        {

            var arg = path + " /t /e /g Все:f";
            ProcessStartInfo pi = new ProcessStartInfo("cacls", arg);
            pi.UseShellExecute = false;
            pi.RedirectStandardOutput = true;
            using (Process process = Process.Start(pi))
            {
                //
                // Read in all the text from the process with the StreamReader.
                //
                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }
        }

        private static void Main(string[] args)
        {
          
            string sSource;
            string sLog;
            string sEvent;

            sSource = "VMDS_Client";
            sLog = "Application";
            sEvent = "Installing....";
            try
            {
                if (!EventLog.SourceExists(sSource))
                    EventLog.CreateEventSource(sSource, sLog);

                EventLog.WriteEntry(sSource, sEvent);
            }
            catch(Win32Exception)
            {
                new EventLog("Application").Clear();
            }

            bool notGUI = args.Length > 0;
            var s = new SoftWare {Productname = "VMDS"};
            s.Update();
            Assembly a = Assembly.GetEntryAssembly();
            string baseDir = Path.GetDirectoryName(a.Location);
            string CurrentDir = baseDir;
            string WithoutGui = "/qn ";
            if (!notGUI) WithoutGui = "";
            string Reinstall = " REINSTALL=ALL REINSTALLMODE=vamus";
            if (!s.Installed) Reinstall = "";
            string msi = "wixsetup.msi";
            if (CurrentDir.Length > 0) msi = "\\" + msi;
            string msiPath = "\""+CurrentDir + msi+"\"";
            string arg = WithoutGui + "/i " + msiPath + Reinstall;
            Console.WriteLine(arg);
            var path = "d:\\virtual";
            try
            {
                CheckDirectories(path);
                SetPermissions(path);
            }
            catch
            {
                Console.WriteLine("Permissions check error... press eny key to exit");
                Console.ReadKey(); 
                Environment.Exit(0);
            }
            Process.Start("msiexec", arg);
        }
    }
}